<?php

class news extends Model {
	private $_Title;
	private $_Subtitle;
	private $_Link;
	private $_Text;
	private $_User;
	private $_Created;
	private $_Type;
	private $_Image;
	private $_Audio;
	private $_Video;
	private $_Tags;
	private $_Comments;
	private $_Url;
	private $_TotalComments;
	private $TYPES = array(
		'interviews',
		'news'
	);

	/**
	 * @return the $_TotalComments
	 */
	public function getTotalComments() {
		return $this->_TotalComments;
	}

	/**
	 * @param field_type $_TotalComments
	 */
	public function setTotalComments($_TotalComments) {
		$this->_TotalComments = $_TotalComments;
	}

	/**
	 * @return the $_Url
	 */
	public function getUrl() {
		return $this->_Url;
	}

	/**
	 * @param field_type $_Url
	 */
	public function setUrl($_Url) {
		$this->_Url = $_Url;
	}

	/**
	 * @return the $_Comments
	 */
	public function getComments() {
		return $this->_Comments;
	}
	
	/**
	 * @param field_type $_Comments
	 */
	public function setComments($_Comments) {
		$this->_Comments = $_Comments;
	}

	/**
	 * @return the $_Audio
	 */
	public function getAudio() {
		return $this->_Audio;
	}

	/**
	 * @return the $_Video
	 */
	public function getVideo() {
		return $this->_Video;
	}

	/**
	 * @param field_type $_Audio
	 */
	public function setAudio($_Audio) {
		$this->_Audio = $_Audio;
	}

	/**
	 * @param field_type $_Video
	 */
	public function setVideo($_Video) {
		$this->_Video = $_Video;
	}

	/**
	 * @param field_type $_Tags
	 */
	public function setTags($_Tags) {
		$this->_Tags = $_Tags;
	}

	private function _getNumberType($name){
		foreach ($this->TYPES as $typePos => $type){
			if ($type == $name)
				return $typePos;
		}
	}
	
	private function _getTypeFromNumber($number){
		return $this->TYPES[$number];
	}
	
	/**
	 * @return the $_Tags
	 */
	public function getTags() {
		return $this->_Tags;
	}

	/**
	 * @return the $_Title
	 */
	public function getTitle() {
		return $this->_Title;
	}

	/**
	 * @return the $_Image
	 */
	public function getImage() {
		return $this->_Image;
	}

	/**
	 * @param field_type $_Image
	 */
	public function setImage($_Image) {
		$this->_Image = $_Image;
	}

	/**
	 * @return the $_Subtitle
	 */
	public function getSubtitle() {
		return $this->_Subtitle;
	}

	/**
	 * @return the $_Link
	 */
	public function getLink() {
		return $this->_Link;
	}

	/**
	 * @return the $_Text
	 */
	public function getText() {
		return $this->_Text;
	}

	/**
	 * @return the $_User
	 */
	public function getUser() {
		return $this->_User;
	}

	/**
	 * @return the $_Created
	 */
	public function getCreated() {
		return $this->_Created;
	}

	/**
	 * @return the $_Type
	 */
	public function _getType() {
		return $this->_Type;
	}

	/**
	 * @param field_type $_Title
	 */
	public function setTitle($_Title) {
		$this->_Title = $_Title;
	}

	/**
	 * @param field_type $_Subtitle
	 */
	public function setSubtitle($_Subtitle) {
		$this->_Subtitle = $_Subtitle;
	}

	/**
	 * @param field_type $_Link
	 */
	public function setLink($_Link) {
		$this->_Link = $_Link;
	}

	/**
	 * @param field_type $_Text
	 */
	public function setText($_Text) {
		$this->_Text = $_Text;
	}

	/**
	 * @param field_type $_User
	 */
	public function setUser($_User) {
		$this->_User = $_User;
	}

	/**
	 * @param field_type $_Created
	 */
	public function setCreated($_Created) {
		$this->_Created = $_Created;
	}
	
	public function getTypeInSpanish(){
		switch($this->_getType()){
			case "interviews":
				return "Entrevista";
			break;
			case "news":
				return "Noticia";
			break;
		}
	}

	/**
	 * @param field_type $_Type
	 */
	public function setType($_Type) {
		$this->_Type = $_Type;
	}
	
	public function setStringType($number){
		$this->setType($this->_getTypeFromNumber($number));
	}
	
	public function getType(){
		return $this->_getNumberType($this->_getType());
	}

	public function __construct($arrInitialValues = null){
		$this->setTable('news');
		$this->setColumnId('news_id');
		parent::__construct($arrInitialValues);
		$this->_setDefaultValues($arrInitialValues);
	}

	public function getResources(){
		$this->setAudio($this->getMyAudios());
		$this->setImage($this->getMyImages());
		$this->setVideo($this->getMyVideos());
		$this->setTags($this->getMyTags());
	}
	
	public function getTheComments($currentPage, $totalComments){
		$arrObjComments = array();

		$arrComments = $this->_getMyComments($currentPage * $totalComments, $totalComments);
		foreach ($arrComments as $comment){
			$objComment = new comment($comment);
			$objComment->setDate(strtotime($objComment->getDate()));
			$arrObjComments[] = $objComment;
		}
		$amount = $this->_getAmountOfComments();
		$this->setTotalComments($amount[0]['amount']);
		$this->setComments($arrObjComments);
	}

	private function _getAmountOfComments(){
		$query = '
			SELECT COUNT(1) AS amount
			FROM comments
			WHERE news_id = ' . $this->getId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	private function _getMyComments($initialComment, $totalComments){
		$query = '
			SELECT *
			FROM comments
			WHERE news_id = ' . $this->getId() . '
			ORDER BY date DESC
			LIMIT ' . $initialComment . ', ' . $totalComments;

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	private function _getResource($table){
		$query = '
			SELECT *
			FROM ' . $table . '
			WHERE news_id = ' . $this->getId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function getMyAudios(){
		$arrAudios = array();
		foreach ($this->_getResource('audio') as $Audio)
			if (file_exists(MAIN_FILES_DIRECTORY . $Audio["name_file"]))
				$arrAudios[] = $Audio;

		return $arrAudios;
	}

	public function getMyVideos(){
		return $this->_getResource('video');
	}

	public function getMyImages(){
		$arrImages = array();
		foreach ($this->_getResource('images') as $Image)
			if (file_exists(MAIN_IMAGES_NEWS_DIRECTORY . $Image["name_file"]))
				$arrImages[] = $Image;

		return $arrImages;
	}
	
	public function getMyTags(){
		return $this->_getResource('tags');
	}

	public function getAll(){
		$query = '
			SELECT *
			FROM news
			WHERE is_news = ' . $this->getType() . '
			ORDER BY ' . $this->getOrderBy() . ' DESC';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function getAllRSS(){
		$query = '
			SELECT *
			FROM news
			ORDER BY ' . $this->getOrderBy() . ' DESC';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}

	public function getCount($count){
		$query = '
			SELECT *
			FROM news
			WHERE is_news = ' . $this->getType() . '
			ORDER BY ' . $this->getOrderBy() . ' DESC
			LIMIT 0, ' . $count;

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function getCountAll($count, $initial = 0){
		$query = '
			SELECT *
			FROM news
			WHERE active = 1
			ORDER BY ' . $this->getOrderBy() . ' DESC
			LIMIT ' . $initial . ', ' . $count;

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function SelectOneImage(){
		$query = '
			SELECT *
			FROM images
			WHERE news_id = ' . $this->getId() . '
			LIMIT 0, 1';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function getWithTags($arrTags){
		$query = '
			SELECT *
			FROM news
			INNER JOIN tags ON tags.news_id = ' . $this->getTable() . '.' . $this->getColumnId() . '
			WHERE tags.tag IN (' . $this->_getWhereQuery($arrTags) . ')
				AND ' . $this->getTable() . ' .active = 1
			GROUP BY ' . $this->getTable() . '.' . $this->getColumnId();

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	public function publishComment($comment){
		$query = '
			INSERT INTO comments
				(comment, writter_name, news_id, date, email)
			VALUES 
				("' . $comment->getComment() . '", "' . $comment->getWritter() . '", ' . $this->getId() . ', "' . date("Y-m-d H:i:s") . '", "' . $comment->getEmail() . '")';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();
	}
	
	private function _getWhereQuery($arrTags){
		$tagsString = "";

		$countTags = count($arrTags);
		for($a = 0; $a < $countTags; $a++){			
			$tagsString .= "'" . $arrTags[$a] . "'";
			if ($a < $countTags - 1)
				$tagsString .= ",";
		}

		return $tagsString;
	}
	
	private function _setDefaultValues($arrInitialValues = null){
		if (isset($arrInitialValues['title']) && !empty($arrInitialValues['title'])){
			$this->setTitle(stripslashes($arrInitialValues['title']));
			$url = preg_replace('/["\',.:]/', '', FunctionsHelper::ReplaceAccentsAndEnie($arrInitialValues['title']));
			$this->setUrl(preg_replace('/[^a-zA-Z0-9]/', '-', $url));
		} else {
			$this->setTitle('');
		}
			
		if (isset($arrInitialValues['subtitle']) && !empty($arrInitialValues['subtitle']))
			$this->setSubtitle(stripslashes($arrInitialValues['subtitle']));
		else 
			$this->setSubtitle('');

		if (isset($arrInitialValues['link']) && !empty($arrInitialValues['link']))
			$this->setLink(stripslashes($arrInitialValues['link']));
		else 
			$this->setLink('');

		if (isset($arrInitialValues['text']) && !empty($arrInitialValues['text']))
			$this->setText(stripslashes($arrInitialValues['text']));
		else 
			$this->setText('');

		if (isset($arrInitialValues['user']) && !empty($arrInitialValues['user']))
			$this->setUser(new user($arrInitialValues['user']));
		else 
			$this->setUser(new user());

		if (isset($arrInitialValues['created']) && !empty($arrInitialValues['created']))
			$this->setCreated($arrInitialValues['created']);
		else 
			$this->setCreated('');

		if (isset($arrInitialValues['type']) && !empty($arrInitialValues['type']))
			$this->setType($arrInitialValues['type']);
		else 
			$this->setType('');
	}

}

?>